<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="日期" prop="time">
              <el-date-picker
                v-model="inputForm.time"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                placeholder="选择日期">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="名称" prop="giftname"
                :rules="[
                 ]">
              <el-input v-model="inputForm.giftname" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数量" prop="giftcount"
                :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.giftcount" placeholder="请填写数量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="快递名称" prop="expressname"
                :rules="[
                 ]">
              <el-select v-model="inputForm.expressname" placeholder="请填写快递名称" style="width: 100%;">
                 <el-option
                    v-for="item in expressnameList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="快递单号" prop="expressnumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.expressnumber" placeholder="请填写快递单号"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="操作人" prop="operator.id"
                :rules="[
                 ]">
                <user-select :limit='1' :value="inputForm.operator.id" @getValue='(value, label) => {inputForm.operator.id=value, inputForm.operator.name=label}'></user-select>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import UserSelect from '@/components/userSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        inputForm: {
          id: '',
          time:'',
          giftname: '',
          giftcount: '',
          expressname: '',
          expressnumber: '',
          operator: {
            id: ''
          },
          remarks: '',
        },
          expressnameList: [{
            value: '中国邮政',
            label: '中国邮政'
          }, {
            value: '顺丰',
            label: '顺丰'
          }, {
            value: '申通',
            label: '申通'
          }, {
            value: '中通',
            label: '中通'
          }, {
            value: '圆通',
            label: '圆通'
          }, {
            value: '极兔',
            label: '极兔'
          }, {
            value: '百世',
            label: '百世'
          }],
      }
    },
    components: {
      UserSelect
    },
    methods: {
      init (method, obj) {
        this.method = method
        if (method === 'add') {
          this.title = `新建客情礼品赠送`
        } else if (method === 'edit') {
          this.title = '修改客情礼品赠送'
        } else if (method === 'view') {
          this.title = '查看客情礼品赠送'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>